package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.dto.MedicineDTO;
import com.demo.dto.OrdersDTO;
import com.demo.entity.*;
import com.demo.service.*;
import com.demo.mapper.OrdersMapper;
import com.demo.util.MyPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Sakura
 * @description 针对表【orders】的数据库操作Service实现
 * @createDate 2023-11-29 10:05:19
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
        implements OrdersService {

    @Autowired
    private UserService userService;

    @Autowired
    private OrderCardService orderCardService;

    @Autowired
    private CardMedicineService cardMedicineService;

    @Autowired
    private MedicineService medicineService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public MyPage<OrdersDTO> queryByPage(Integer pageNo, Integer pageSize, Long uid) {
        Page<Orders> page = this.page(new Page<>(pageNo, pageSize));
        List<OrdersDTO> ordersDTOList = page.getRecords().stream().map(item -> {
            OrdersDTO ordersDTO = new OrdersDTO();
            BeanUtils.copyProperties(item, ordersDTO);
            User user = userService.getById(item.getUserId());
            User checkUser = userService.getById(item.getCheckUser());
            ordersDTO.setBuyUserName(user.getName());
            ordersDTO.setPhone(user.getPhone());
            if(checkUser!=null){
                ordersDTO.setCheckUserName(checkUser.getName());
            }
            return ordersDTO;
        }).collect(Collectors.toList());

        return new MyPage(ordersDTOList, page.getTotal());
    }

    /**
     * 根据订单id获取药品列表
     *
     * @param id:
     * @return Object
     * @author Sakura
     * @date 2023/11/29 11:10
     */
    @Override
    public Map<String, Object> getMedicineListById(Long id) {
        Map<String, Object> result = new HashMap<>();
        result.put("id", id);
        LambdaQueryWrapper<OrderCard> orderCardWrapper = new LambdaQueryWrapper<>();
        orderCardWrapper.eq(OrderCard::getOrderId, id);
        List<OrderCard> medicinesList = orderCardService.list(orderCardWrapper);
        double totalPrice = 0;
        if (medicinesList.size() == 0) {
            result.put("list", new ArrayList<>());
            result.put("totalPrice", totalPrice);
            return result;
        }
        List<MedicineDTO> dtoList = new ArrayList<>();
        for (OrderCard orderCard : medicinesList) {
            Medicine medicine = medicineService.getById(orderCard.getMedicineId());
            MedicineDTO dto = new MedicineDTO();
            BeanUtils.copyProperties(medicine, dto);
            dto.setSum(orderCard.getNum());
            if (orderCard.getNum() != null&&orderCard.getNum()>=0) {
                totalPrice += medicine.getPrice()*orderCard.getNum();
            }
            dtoList.add(dto);
        }
        result.put("list", dtoList);
        result.put("totalPrice", totalPrice);
        Orders orders = this.getById(id);
        result.put("image", orders.getImage());
        return result;
    }

//    @Override
//    public Map<String,Object> getMedicineListById(Long id) {
//        Map<String, Object> result = new HashMap<>();
//        result.put("id", id);
//        LambdaQueryWrapper<OrderCard> orderCardWrapper = new LambdaQueryWrapper<>();
//        orderCardWrapper.eq(OrderCard::getOrderId, id);
//        //先查出 订单，再查出购物车
//        OrderCard orderCard = orderCardService.getOne(orderCardWrapper);
//        //购物车id
//        Long cardId = orderCard.getCardId();
//        //根据购物车查出药品列表
//        LambdaQueryWrapper<CardMedicine> cardMedicineWrapper = new LambdaQueryWrapper<>();
//        cardMedicineWrapper.eq(CardMedicine::getCardId, cardId);
//        List<CardMedicine> cardMedicineList = cardMedicineService.list(cardMedicineWrapper);
//
//        double totalPrice = 0;
//        //先算一下总价格
//        for (CardMedicine item : cardMedicineList) {
//            Medicine medicine = medicineService.getById(item.getMedicineId());
//            //购买数量
//            Integer sum = item.getSum();
//            totalPrice += (sum*medicine.getPrice());
//
//        }
//
//        result.put("totalPrice", totalPrice);
//        //订单的药品集合
//        List<Long> medicineIds = cardMedicineList.stream().map(item -> item.getMedicineId()).collect(Collectors.toList());
//
//        if (medicineIds.size() == 0) {
//            result.put("records", new ArrayList<>());
//            return result;
//        }
//        List<Medicine> medicines = medicineService.listByIds(medicineIds);
//        List<MedicineDTO> medicineDTOList = medicines.stream().map(item -> {
//            MedicineDTO dto = new MedicineDTO();
//            BeanUtils.copyProperties(item, dto);
//            Category category = categoryService.getById(item.getCategoryId());
//            dto.setCategory(category.getName());
//
//            LambdaQueryWrapper<CardMedicine> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(CardMedicine::getCardId, cardId).eq(CardMedicine::getMedicineId, item.getId());
//            CardMedicine cardMedicine = cardMedicineService.getOne(wrapper);
//
//            //查询数量
//            dto.setSum(cardMedicine.getSum());
//            return dto;
//        }).collect(Collectors.toList());
//        result.put("records",medicineDTOList);
//        return result;
//    }

}




